iDigi Device Connector API

Alarms Code Sample

Here is some sample code that allows users to add email alarms to the Device Connector. The check_alarms.trigger adds a database trigger to determine if alarms are tripped. The Alarms.cls class extends the Standard Controller class used for the VisualForce page, Alarm.page. The Alarm.page VisualForce page allows users to edit and clear alarms.

 

check_alarms.trigger

 

trigger check_alarms on idigi__Dia_Reading__c (after insert) {

    Set<Id> channels = new Set<Id>();

    for (Dia_Reading__c r :Trigger.new) {

        channels.add( r.Dia_Channel__c);

    }

   

    if (! channels.isEmpty()) {

        // Get a list of all untripped alarms for these readings' channels.

        Alarm__c[] alarms = [

            SELECT Dia_Channel__c, User__r.Email,

                   Comparison__c, Value__c, Tripped__c,

                   Dia_Channel__r.Name, Dia_Channel__r.Dia_Instance__r.Name

            FROM Alarm__c

            WHERE Dia_Channel__c IN :channels AND Tripped__c = False];

       

        // keep track of alarms that trigger, and upsert them when done

        Alarm__c[] updated_alarms = new Alarm__c[]{};

       

        // list of emails to send

        Messaging.SingleEmailMessage[] emails = new Messaging.SingleEmailMessage[]{};

       

        for (Alarm__c a : alarms) {

            String op = a.Comparison__c;  // short name for comparison operator

           

            // Look at the inserted readings to see if alarm should trigger.

            for (Dia_Reading__c r :Trigger.new) {

                if (r.Dia_Channel__c == a.Dia_Channel__c) {

                    Boolean trip = False;

                   

                    if (op == '<')  trip = r.Float_Value__c < a.Value__c;

                    else if (op == '>')  trip = r.Float_Value__c > a.Value__c;

                    else if (op == '<=') trip = r.Float_Value__c <= a.Value__c;

                    else if (op == '>=') trip = r.Float_Value__c >= a.Value__c;

                    else if (op == '==') trip = r.Float_Value__c == a.Value__c;

                    else if (op == '!=') trip = r.Float_Value__c != a.Value__c;

                    

                    if (trip) {

                        // Mark alarm as tripped in the database.

                        a.Tripped__c = True;

                        updated_alarms.add( a);

                        emails.add( Alarm.newMail( a, r));

                        break;

                    }

                }

            }

        }

       

        upsert updated_alarms;

        Messaging.sendEmail( emails);

    }

}

 

Alarm.cls

 

public with sharing class Alarm {

    /** Method called from trigger to send an email Alarm. */

    public static Messaging.SingleEmailMessage newMail( Alarm__c a,

                                                        Dia_Reading__c r) {

        Messaging.SingleEmailMessage m = new Messaging.SingleEmailMessage();

       

        m.setToAddresses( new String[]{ a.User__r.Email });

        m.setSenderDisplayName( 'iDigi Alarm');

        m.setSubject( 'Alarm on ' + a.Dia_Channel__r.Dia_Instance__r.Name +

                      '/' + a.Dia_Channel__r.Name);

        m.setPlainTextBody( 'Dia Reading of ' + r.Float_Value__c + ' taken at '

                            + r.Update_Time__c.format() + ' triggered alarm "'

                            + a.Comparison__c + ' ' + a.Value__c + '".\n\n'

                            + 'Visit '

                            + URL.getSalesforceBaseUrl().toExternalForm()

                            + '/' + a.Dia_Channel__c + ' for latest readings.');

       

        return m;

    }

   

    /// Object referenced by Alarm.page.

    private final Alarm__c alarm { get; set; }

   

    public Alarm( ApexPages.StandardController stdController) {

        alarm = (Alarm__c) stdController.getRecord();

        if (alarm.User__c == Null) {

            alarm.User__c = UserInfo.getUserId();   // default to current user

        }

    }

   

    /// Method called when save button is clicked on Alarm.page.

    public PageReference save() {

        try {

            upsert alarm;

        } catch (DMLException e) {

            ApexPages.addMessage( new ApexPages.message(

                ApexPages.severity.ERROR, 'Error saving Alarm record.'));

            return Null;        // stay on current page

        }

       

        return detailPage();

    }

   

    /// Method called when cancel button is clicked on Alarm.page.

    public PageReference detailPage() {

        PageReference alarmPage;

        try {

            String ret = ApexPages.currentPage().getParameters().get('retUrl');

            alarmPage = new PageReference( ret);

        } catch (Exception e) {

            // Redirect to the Alarm Detail page if no retUrl.

            alarmPage = new ApexPages.StandardController( alarm).view();

        }

        alarmPage.setRedirect( True);

        return alarmPage;

    }

}

 

Alarm.page

 

<apex:page standardController="Alarm__c" extensions="Alarm">

    <apex:form >

      <apex:pageMessages /> <!-- error messages -->

      <apex:pageBlock title="Configure Alarm" mode="edit">

        <apex:pageBlockSection showHeader="false" columns="1">

            <apex:inputField value="{!Alarm__c.User__c}" />

            <apex:inputField value="{!Alarm__c.Dia_Channel__c}" />

           

            <!-- Extra markup to get the red required line on

                 inputSecret instead of inputField -->

            <apex:pageBlockSectionItem >

              <apex:outputLabel value="Condition" />

              <apex:outputPanel styleClass="requiredInput" layout="block">

                <apex:outputPanel styleClass="requiredBlock" layout="block" />

                <apex:outputText value="Float_Value"/>

                <apex:inputField value="{!Alarm__c.Comparison__c}"/>

                <apex:inputField value="{!Alarm__c.Value__c}"/>

              </apex:outputPanel>

            </apex:pageBlockSectionItem>

 

            <apex:inputField value="{!Alarm__c.Tripped__c}" />

        </apex:pageBlockSection>

        <apex:pageBlockButtons >

            <apex:commandButton action="{!save}" value="Save"/>

            <apex:commandButton action="{!detailPage}" value="Cancel"

                immediate="true"/>

        </apex:pageBlockButtons>

      </apex:pageBlock>

    </apex:form>

</apex:page>

Home

 

Digi Logo Small Copyright ©2012 Digi International Inc. All rights reserved.
90002179_A